#include "TipLayer.h"
#include "ConstClass.h"
#include "PlayerInfo.h"
#include "PlayLayer.h"
#include "AudioManage.h"
#include "StartLayer.h"

#define PNG_BACK "Tip/tips.png"
#define PNG_TITLE "Tip/title.png"
#define PNG_ROUND "Tip/tip_round.png"
#define PNG_RESHUFFLE "Tip/tip_reShuffle.png"
#define PNG_WINNER "Tip/tip_winner.png"
#define PNG_SYMBOL_RED "Start/symbol_red.png"
#define PNG_SYMBOL_YELLOW "Start/symbol_yellow.png"
#define PNG_SYMBOL_BLUE "Start/symbol_blue.png"
#define PNG_SYMBOL_GREEN "Start/symbol_green.png"
#define PNG_SYMBOL_PURPLE "Start/symbol_purple.png"
#define PNG_YOU_WIN "Tip/tip_youwin.png"
#define PNG_YOU_LOSE "Tip/tip_youlose.png"
#define PNG_BACKGROUND "Card/colorCard_back.png"
#define BMF_NAME_FNT "fonts/helvetica-32.fnt"
#define PNG_EXIT "Tip/exit.png"
#define PNG_NO_WINNER "Tip/tip_nowinner.png"

#define TIME_PAUSE 0.8f
#define TIME_MOVE 0.2f
#define TIME_FADEIN 0.2f
#define TIME_FADEOUT 0.5f

TipLayer::TipLayer()
{
}

TipLayer::~TipLayer()
{
}

bool TipLayer::init()
{
	Layer::init();

	Size size = Director::getInstance()->getVisibleSize();

	Sprite* backSprite = Sprite::create(PNG_BACK);
	backSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.94f));
	this->addChild(backSprite, 0);

	this->titleSprite = Sprite::create(PNG_TITLE);
	titleSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.94f));
	this->addChild(titleSprite, 1);

	this->roundSprite = Sprite::create(PNG_ROUND);
	roundSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.94f));
	roundSprite->setVisible(false);
	roundSprite->setScale(0.9f);
	this->addChild(roundSprite, 1);

	this->currentSprite = titleSprite;

	this->currentPlayer = Label::createWithBMFont(BMF_NAME_FNT, "");
	currentPlayer->setPosition(Vec2(size.width * 0.33f, size.height * 0.94f));
	currentPlayer->setScale(0.6f);
	currentPlayer->setVisible(false);
	this->addChild(currentPlayer, 1);
	
	this->reShuffleSprite = Sprite::create(PNG_RESHUFFLE);
	reShuffleSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.94f));
	reShuffleSprite->setVisible(false);
	reShuffleSprite->setScale(0.9f);
	this->addChild(reShuffleSprite, 1);

	this->winnerSprite = Sprite::create(PNG_WINNER);
	winnerSprite->setPosition(Vec2(size.width * 0.5f, size.height * 0.94f));
	winnerSprite->setVisible(false);
	winnerSprite->setScale(0.9f);
	this->addChild(winnerSprite, 1);

	return true;
}

void TipLayer::roundTip()
{
	this->currentSprite->runAction(Sequence::create(
		FadeOut::create(TIME_FADEOUT),
		Hide::create(),
		NULL
		));
	this->currentPlayer->runAction(Sequence::create(
		FadeOut::create(TIME_FADEOUT),
		Hide::create(),		
		NULL
		));

	this->currentPlayer->setString(PlayerInfo::Players[ConstClass::PLAYING_P_ID]->name);

	if (ConstClass::PLAYING_P_ID == ConstClass::MY_P_ID)
	{
		AudioManage::myTurnSound();
	}

	this->currentPlayer->runAction(Sequence::create(
		DelayTime::create(TIME_FADEOUT),
		Show::create(),
		FadeIn::create(TIME_FADEIN),
		NULL
		));

	this->roundSprite->runAction(Sequence::create(
		DelayTime::create(TIME_FADEOUT),
		Show::create(),
		FadeIn::create(TIME_FADEIN),
		NULL
		));

	this->currentSprite = roundSprite;
}

void TipLayer::reShuffleTip()
{
	AudioManage::shuffleSound();

	this->currentSprite->runAction(Sequence::create(
		FadeOut::create(TIME_FADEOUT),
		Hide::create(),
		NULL
		));
	this->currentPlayer->runAction(Sequence::create(
		FadeOut::create(TIME_FADEOUT),
		Hide::create(),
		NULL
		));

	this->reShuffleSprite->runAction(Sequence::create(
		DelayTime::create(TIME_FADEOUT),
		Show::create(),
		FadeIn::create(TIME_FADEIN),
		NULL
		));

	this->currentSprite = reShuffleSprite;
}

void TipLayer::gameOverTip()
{
	AudioManage::stopPlayMusic();

	this->currentSprite->runAction(Sequence::create(
		FadeOut::create(TIME_FADEOUT),
		Hide::create(),
		NULL
		));
	this->currentPlayer->runAction(Sequence::create(
		FadeOut::create(TIME_FADEOUT),
		Hide::create(),
		NULL
		));

	if (ConstClass::WINNER_P_ID != -1)
	{
		this->currentPlayer->setString(PlayerInfo::Players[ConstClass::WINNER_P_ID]->name);

		this->currentPlayer->runAction(Sequence::create(
			DelayTime::create(TIME_FADEOUT),
			Show::create(),
			FadeIn::create(TIME_FADEIN),
			NULL
			));

		this->winnerSprite->runAction(Sequence::create(
			DelayTime::create(TIME_FADEOUT),
			Show::create(),
			FadeIn::create(TIME_FADEIN),
			NULL
			));
	}
	else
	{
		Size size = Director::getInstance()->getVisibleSize();

		Sprite* nowinner = Sprite::create(PNG_NO_WINNER);
		nowinner->setPosition(Vec2(size.width * 0.5f, size.height * 0.94f));
		nowinner->setVisible(false);

		this->addChild(nowinner, 10);

		nowinner->runAction(Sequence::create(
			DelayTime::create(TIME_FADEOUT),
			Show::create(),
			FadeIn::create(TIME_FADEIN),
			NULL
			));
	}

	Size size = Director::getInstance()->getVisibleSize();
	Sprite* background = Sprite::create(PNG_BACKGROUND);
	background->setPosition(Vec2(size.width * 0.5f, size.height * 0.55f));
	background->setVisible(false);
	this->addChild(background, 10);

	std::string symbolImages[5] = {PNG_SYMBOL_RED, PNG_SYMBOL_YELLOW, PNG_SYMBOL_BLUE, 
	PNG_SYMBOL_GREEN, PNG_SYMBOL_PURPLE};

	Sprite* symbol = Sprite::create(symbolImages[ConstClass::MY_T_INDEX]);
	symbol->setPosition(Vec2(size.width * 0.33f, size.height * 0.5f));
	symbol->setVisible(false);
	this->addChild(symbol, 10);

	Sprite* conclusion = Sprite::create();
	if (ConstClass::MY_P_ID == ConstClass::WINNER_P_ID )
	{
		AudioManage::winSound();
		conclusion->setTexture(PNG_YOU_WIN);
	}
	else
	{
		AudioManage::loseSound();
		conclusion->setTexture(PNG_YOU_LOSE);
	}
	conclusion->setPosition(Vec2(size.width * 0.5f, size.height * 0.63f));
	conclusion->setVisible(false);
	this->addChild(conclusion, 10);

	MenuItemImage* exitItem = MenuItemImage::create(PNG_EXIT, PNG_EXIT, CC_CALLBACK_0(TipLayer::backToMenu, this));
	exitItem->setPosition(Vec2(size.width * 0.7f, size.height * 0.48f));

	Menu* startMenu = Menu::create(exitItem, NULL);
	startMenu->setPosition(Vec2::ZERO);
	this->addChild(startMenu, 10);

	Node* nodes[4] = { background, symbol, conclusion, startMenu };
	for (int i = 0; i < 4; i++)
	{
		nodes[i]->runAction(Sequence::create(
			FadeOut::create(0),
			Show::create(),
			DelayTime::create(1.6f),
			FadeIn::create(0.5f),
			NULL
			));
	}
}

void TipLayer::backToMenu(Ref* pSender)
{
	auto scene = StartLayer::createScene();

	auto director = Director::getInstance();
	director->replaceScene(scene);
}